﻿using Coldairarrow.Entity.AM;
using Coldairarrow.Entity.Base_Manage;
using Coldairarrow.Entity.DTO;
using Coldairarrow.Util;
using EFCore.Sharding;
using LinqKit;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Linq.Expressions;
using System.Threading.Tasks;

namespace Coldairarrow.Business.AM
{
    public partial class am_assetBusiness : BaseBusiness<AM_Asset>, Iam_assetBusiness, ITransientDependency
    {
        public am_assetBusiness(IDbAccessor db)
            : base(db)
        {
        }

        #region 外部接口

        public async Task<PageResult<AM_Asset>> GetDataListAsync(PageInput<ConditionDTO> input)
        {
            var q = GetIQueryable();
            var where = LinqHelper.True<AM_Asset>();
            var search = input.Search;

            //筛选
            if (!search.Condition.IsNullOrEmpty() && !search.Keyword.IsNullOrEmpty())
            {
                var newWhere = DynamicExpressionParser.ParseLambda<AM_Asset, bool>(
                    ParsingConfig.Default, false, $@"{search.Condition}.Contains(@0)", search.Keyword);
                where = where.And(newWhere);
            }

            return await q.Where(where).GetPageResultAsync(input);
        }

        public async Task<AM_Asset> GetTheDataAsync(string id)
        {
            return await GetEntityAsync(id);
        }

        public async Task AddDataAsync(AM_Asset data)
        {
            await InsertAsync(data);
        }

        public async Task UpdateDataAsync(AM_Asset data)
        {
            await UpdateAsync(data);
        }

        public async Task DeleteDataAsync(List<string> ids)
        {
            await DeleteAsync(ids);
        }
        /// <summary>
        /// 通过资产二维码获取资产信息
        /// </summary>
        /// <param name="QrCode">二维码</param>
        /// <returns></returns>
        public async Task<Am_assetDto> GetQrCodeAsset(string QrCode)
        {
            string code = QrCode.Split(',')[0].ToString();
            if (code == "40")
            {
                Expression<Func<AM_Asset, AM_AssetItem, Base_Department, Base_Department, AM_AssetCategory, Base_User, Am_assetDto>> select = (a, b,c,d,e,f) => new Am_assetDto
                {
                    ResponsibleDeptName = c.Name,
                    AssetPositionName=d.Name,
                    TypeName=e.AssetCategory,
                    UserName=f.RealName,
                };
                select = select.BuildExtendSelectExpre();
                var q = from a in GetIQueryable().AsExpandable()
                        join b in Db.GetIQueryable<AM_AssetItem>() on a.AssetId equals b.ParentID into ab
                        from b in ab.DefaultIfEmpty()
                        join c in Db.GetIQueryable<Base_Department>() on a.ResponsibleDept equals c.Id into ac
                        from c in ac.DefaultIfEmpty()
                        join d in Db.GetIQueryable<Base_Department>() on a.AssetPosition equals d.Id into ad
                        from d in ad.DefaultIfEmpty()
                        join e in Db.GetIQueryable<AM_AssetCategory>() on a.AssetCategoryId equals e.ID into ae
                        from e in ae.DefaultIfEmpty()
                        join f in Db.GetIQueryable<Base_User>() on a.Custodian equals f.Id into af
                        from f in af.DefaultIfEmpty()
                        select @select.Invoke(a, b,c,d,e,f);
                string Code= QrCode.Split(',')[1].ToString();
                var data =await q.Where(m=>m.AssetCode== Code).FirstOrDefaultAsync();
                if (data == null)
                {
                    throw new BusException("资产不存在");
                }
                else {
                    return data;
                }
            }
            else {
                throw new BusException("请扫描正确的资产二维码");
            }
            
        }

        #endregion

        #region 私有成员

        #endregion
    }
}